package com.itheima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private EmpExprService empExprService;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmpAndEmpExpr(Emp emp) {
        this.save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(v -> v.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
    }

    @Override
    public Result getPageResult(EmpQueryParam param) {
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());
        LambdaQueryWrapper<Emp> lqw = Wrappers.<Emp>lambdaQuery()
                .like(StrUtil.isNotEmpty(param.getName()), Emp::getName, param.getName())
                .eq(ObjectUtil.isNotEmpty(param.getGender()), Emp::getGender, param.getGender())
                .between(ObjectUtil.isNotEmpty(param.getBegin()) && ObjectUtil.isNotEmpty(param.getEnd())
                        , Emp::getEntryDate, param.getBegin(), param.getEnd());
        this.page(page, lqw);
        List<Emp> emps = page.getRecords();

        Set<Integer> deptIds = emps.stream().map(Emp::getDeptId).collect(Collectors.toSet());
        List<Dept> depts = deptMapper.selectBatchIds(deptIds);
        Map<Integer, String> deptMap = depts.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
        emps.forEach(v -> v.setDeptName(deptMap.get(v.getDeptId())));
        PageResult<Emp> pageResult = new PageResult<>(page.getTotal(), emps);
        return Result.success(pageResult);
    }

    @Override
    @Transactional(readOnly = true)
    public Result getEmpAndEmpExprById(Integer id) {
        Emp emp = this.getById(id);
        List<EmpExpr> empExprList = empExprMapper.selectList(Wrappers.<EmpExpr>lambdaQuery().eq(EmpExpr::getEmpId, id));
        emp.setExprList(empExprList);
        emp.setDeptName(deptMapper.selectById(emp.getDeptId()).getName());
        return Result.success(emp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteEmpAndEmpExprByIds(List<Integer> ids) {
        this.removeByIds(ids);
        empExprService.remove(Wrappers.<EmpExpr>lambdaQuery().in(EmpExpr::getEmpId,ids));
        return Result.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateEmpAndEmpExpr(Emp emp) {
        this.updateById(emp);
        empExprService.remove(Wrappers.<EmpExpr>lambdaQuery().in(EmpExpr::getEmpId,emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(v->v.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
        return Result.success();
    }


}
